package com.codeoss.module.purchase.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.module.purchase.dao.PurchaseOrderLineMapper;
import com.codeoss.module.purchase.model.PurchaseOrderLine;
import com.codeoss.module.purchase.service.IPurchaseOrderLineService;
import com.codeoss.module.uom.service.IUomUomConversionService;

@Service
public class PurchaseOrderLineService extends BaseService<PurchaseOrderLineMapper, PurchaseOrderLine>
		implements IPurchaseOrderLineService {
	@Autowired
    private IUomUomConversionService uomUomConversionService;
	
	@Transactional
	public void insert(PurchaseOrderLine purchaseOrderLine) {
		beforeSave(purchaseOrderLine,true);
		super.insert(purchaseOrderLine);
	}
	
	@Transactional
	public void update(PurchaseOrderLine purchaseOrderLine) {
		beforeSave(purchaseOrderLine,false);
		super.update(purchaseOrderLine);
	}
	
	@Transactional
	protected void beforeSave(PurchaseOrderLine purchaseOrderLine,boolean isNewRecord) {
		Long productId=purchaseOrderLine.getProductProductId();
	    BigDecimal qtyEntered=purchaseOrderLine.getQtyEntered();
	    Long uomUomId=purchaseOrderLine.getUomUomId();
	    if(productId!=null&&qtyEntered!=null&&uomUomId!=null) {
	    	BigDecimal qtyOrdered=uomUomConversionService.convert(productId, uomUomId, qtyEntered);
	    	purchaseOrderLine.setQtyOrdered(qtyOrdered);
	    }
	}

	@Override
	public List<PurchaseOrderLine> getLineListByPurchaseOrderId(Long purchaseOrderId) {
		// TODO Auto-generated method stub
		QueryWrapper<PurchaseOrderLine> query=new QueryWrapper<PurchaseOrderLine>();
		query.eq("purchase_order_id", purchaseOrderId);
		return list(query);
	}
}